package com.rabbit.rebbitmqextension.listener;


import com.rabbit.rebbitmqextension.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;



@Component
public class ExtenListener {

    @RabbitListener(queues = Constants.NONE_QUEUE)
    public void noneListen(Message msg){
        String message = new String(msg.getBody());
        System.out.println("none 收到消息"+message);
//        int a=1/0;
        System.out.println("none 响应结束");
    }

    @RabbitListener(queues = Constants.AUTO_QUEUE)
    public void autoListen(Message msg){
        String message = new String(msg.getBody());
        System.out.println("auto 收到消息"+message);
//        int a=1/0;
        System.out.println("auto 响应结束");
    }

    @RabbitListener(queues = Constants.MANUAL_QUEUE)
    public void manualListen(Message msg, Channel channel) throws Exception {
        long deliveryTag = msg.getMessageProperties().getDeliveryTag();
        try{
            String message = new String(msg.getBody());
            System.out.println("manual 收到消息"+message);
            int a=1/0;
            System.out.println("manual 响应结束");
            //  手动发送 ack 进行 确认
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            //  basicNack(long deliveryTag, boolean multiple, boolean requeue)
            channel.basicNack(deliveryTag,false,true);
        }

    }

    @RabbitListener(queues = Constants.CONFIRM_QUEUE)
    public void confirmListen(Message msg, Channel channel) throws Exception {
        String message = new String(msg.getBody());

        System.out.println("confirm 收到的 消息 ： "+ message );
    }

    @RabbitListener(queues = Constants.RETURN_QUEUE)
    public void returnListen(Message msg) throws Exception {
        String message = new String(msg.getBody());

        System.out.println("return 收到的 消息 ： "+ message );
    }
}
